home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / JavaIDL-EA-win32.EXE / JavaIDL / examples / portfolio / PortfolioServer.java < prev    next >
Encoding:
Java Source  |  1997-03-05  |  6.1 KB  |  235 lines

  1. /*
  2.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  5.  * modify and redistribute this software in source and binary code form,
  6.  * provided that i) this copyright notice and license appear on all copies of
  7.  * the software; and ii) Licensee does not utilize the software in a manner
  8.  * which is disparaging to Sun.
  9.  *
  10.  * This software is provided "AS IS," without a warranty of any kind. ALL
  11.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  12.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  13.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  14.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  15.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  16.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  17.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  18.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  19.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  20.  * POSSIBILITY OF SUCH DAMAGES.
  21.  *
  22.  * This software is not designed or intended for use in on-line control of
  23.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  24.  * the design, construction, operation or maintenance of any nuclear
  25.  * facility. Licensee represents and warrants that it will not use or
  26.  * redistribute the Software for such purposes.
  27.  */
  28.  
  29. /**
  30.  * This class is the Portfolio Server implementation
  31.  * @author Rahul
  32.  */
  33.  
  34. import java.awt.*;
  35. import java.util.*;
  36. import java.net.*;
  37. import java.io.*;
  38.  
  39. import PortfolioManager.*;
  40.  
  41. import com.sun.CORBA.iiop.*;
  42. import org.omg.CORBA.*;
  43. import org.omg.CosNaming.*;
  44. import org.omg.CosNaming.NamingContextPackage.*;
  45.  
  46.  
  47. /** Servant class implementation for Portfolio
  48. **/
  49. class 
  50. PortfolioServantImpl extends PortfolioManager._PortfolioImplBase {
  51.  
  52.   /** Map for Stocks in Portfolio
  53.   **/
  54.   private Hashtable stocksMap_;
  55.  
  56.   /** Constructor
  57.   **/
  58.   public 
  59.   PortfolioServantImpl() {
  60.     stocksMap_ = new Hashtable();
  61.   }
  62.  
  63.   public
  64.   PortfolioManager.Stock addStock(String ticker, short number, double price)
  65.   {
  66.     
  67.     StockServantImpl _servant = new StockServantImpl(ticker, number, price);
  68.     if (stocksMap_.containsKey(ticker) == true) {
  69.       System.out.println("Portfolio: Already contains Stock with ticker " +
  70.                             ticker);
  71.       return null;
  72.     }
  73.     else {
  74.       stocksMap_.put(ticker, _servant);
  75.       PortfolioServer.theOrb_.connect(_servant);
  76.       return _servant;
  77.     }
  78.   }
  79.  
  80.   public
  81.   void deleteStock(String ticker) {
  82.     stocksMap_.remove(ticker);
  83.   }
  84.  
  85.   public
  86.   PortfolioManager.Stock getStock(String ticker) {
  87.     StockServantImpl _servant = (StockServantImpl)stocksMap_.get(ticker);
  88.     PortfolioServer.theOrb_.connect(_servant);
  89.     return _servant;
  90.   }
  91.  
  92.   public
  93.   void buy(String ticker, short number) {
  94.     if (stocksMap_.containsKey(ticker) == true) {
  95.       StockServantImpl _servant = (StockServantImpl)stocksMap_.get(ticker);
  96.       _servant.number_ += number;
  97.     }
  98.     else {
  99.       System.out.println("PortfolioServer: Invalid Transaction");
  100.     }
  101.   }
  102.  
  103.   public
  104.   void sell(String ticker, short number) {
  105.     if (stocksMap_.containsKey(ticker) == true) {
  106.       StockServantImpl _servant = (StockServantImpl)stocksMap_.get(ticker);
  107.       if (_servant.number_ >= number) {
  108.         _servant.number_ -= number;
  109.       }
  110.     }
  111.     else {
  112.       System.out.println("PortfolioServer: Invalid Transaction");
  113.     }
  114.   }
  115.  
  116.   public
  117.   double value() {
  118.     
  119.     double totalValue = 0;
  120.     
  121.     Enumeration stocks = stocksMap_.elements();
  122.     while (stocks.hasMoreElements() == true) {
  123.       StockServantImpl stock = (StockServantImpl)stocks.nextElement();
  124.       totalValue += stock.number_ * stock.price_;
  125.     }
  126.  
  127.     return totalValue;
  128.   }
  129.  
  130.   public
  131.   String report() {
  132.     
  133.     StringBuffer _report = new StringBuffer();
  134.     Enumeration stocks = (Enumeration)stocksMap_.elements();
  135.     while (stocks.hasMoreElements() == true) {
  136.       StockServantImpl stock = (StockServantImpl)stocks.nextElement();
  137.       _report.append(stock.toString()).append("\n");
  138.     }
  139.     return _report.toString();
  140.   }
  141.  
  142.  
  143. /** Servant Class implementation for Stock
  144. **/
  145. class
  146. StockServantImpl extends PortfolioManager._StockImplBase {
  147.   /** Stock Data
  148.   **/
  149.   public String ticker_;
  150.   public short  number_;
  151.   public double  price_; 
  152.  
  153.   /** Constructor
  154.   **/
  155.   public
  156.   StockServantImpl(String ticker, short number, double price) {
  157.     ticker_ = ticker;
  158.     number_ = number;
  159.     price_  = price;
  160.   }   
  161.  
  162.   
  163.   /** print 
  164.   **/
  165.   public
  166.   String toString() {
  167.     StringBuffer sb = new StringBuffer();
  168.     sb.append(this.ticker_).append(": ").append("Number: ").
  169.        append(this.number_).append(" Price: ").append(this.price_);
  170.     return sb.toString();
  171.   } 
  172.  
  173.   public
  174.   String ticker() {
  175.     return this.ticker_;
  176.   }
  177.  
  178.   public
  179.   short number() {
  180.     return this.number_;
  181.   }
  182.  
  183.   public
  184.   double price() {
  185.     return this.price_;
  186.   }
  187.  
  188.   public
  189.   void price(double arg) {
  190.     price_ = arg;
  191.   }
  192.  
  193. }
  194.  
  195.  
  196. public class PortfolioServer {
  197.  
  198.   public static org.omg.CORBA.ORB theOrb_;
  199.  
  200.   /** Main
  201.   **/
  202.   public static void main(String[] args) {
  203.     try {
  204.       // create and initialize ORB
  205.       theOrb_ =  org.omg.CORBA.ORB.init(args, null);
  206.  
  207.       // create Servant and register it with ORB
  208.       PortfolioServantImpl servant = new PortfolioServantImpl();
  209.       theOrb_.connect(servant);
  210.  
  211.       // get the root naming context
  212.       org.omg.CORBA.Object objRef =
  213.                 theOrb_.resolve_initial_references("NameService");
  214.       NamingContext ncRef = NamingContextHelper.narrow(objRef);
  215.  
  216.       // bind the Object Reference in Naming
  217.       NameComponent nc = new NameComponent("Portfolio", "");
  218.       NameComponent path[] = {nc};
  219.       ncRef.rebind(path, servant);
  220.  
  221.       // wait for invocations from clients
  222.       java.lang.Object sync = new java.lang.Object();
  223.       synchronized (sync) {
  224.          sync.wait();
  225.       }
  226.     } 
  227.     catch (Exception ex) {
  228.       ex.printStackTrace();
  229.       System.err.println("Portfolio Server: Exception-> " + ex);
  230.     }   
  231.   }
  232.  
  233. }
  234.